package com.taiji.web.controller.workOrder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;

import com.taiji.common.config.RuoYiConfig;
import com.taiji.common.utils.file.FileUploadUtils;
import com.taiji.workOrder.domain.OrderEnclosure;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.taiji.common.annotation.Log;
import com.taiji.common.core.controller.BaseController;
import com.taiji.common.core.domain.AjaxResult;
import com.taiji.common.enums.BusinessType;
import com.taiji.workOrder.domain.Report;
import com.taiji.workOrder.service.IReportService;
import com.taiji.common.utils.poi.ExcelUtil;
import com.taiji.common.core.page.TableDataInfo;

/**
 * 报告信息Controller
 *
 * @author taiji
 * @date 2023-08-08
 */
@RestController
@RequestMapping("/workOrder/report")
@Api(tags="报告管理报告信息")
public class ReportController extends BaseController
{
    @Autowired
    private IReportService reportService;

    private static String defaultBaseDir = RuoYiConfig.getProfile()+"/upload";


    /**
     * 查询报告附件列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:OrderEnclosure:list')")
    @GetMapping("/listEnclosure")
    @ApiOperation("查询报告附件列表")
    public TableDataInfo listEnclosure(Report report)
    {
        System.out.println("参数: "+report);
        startPage();
        List<Report> list = reportService.selectReportEnclosureList(report);
        return getDataTable(list);
    }

    /**
     * 导出工单附件列表
     *
     * @return
     */
    @Log(title = "导出报告附件", businessType = BusinessType.EXPORT)
    @PostMapping("/exportEnclosure")
    @PermitAll
    @ApiOperation("批量导出")
    public void exportEnclosure(HttpServletResponse response, Report report) throws Exception{
        List<Report> list = reportService.selectReportEnclosureList(report);

        response.setContentType("application/zip");
        response.setCharacterEncoding("utf-8");

        try (ZipOutputStream zip = new ZipOutputStream(response.getOutputStream())) {
            for (Report item : list) {

                File file = FileUploadUtils.getAbsoluteFile(defaultBaseDir,item.getUrl().substring(item.getUrl().lastIndexOf("/") + 1));
                ZipEntry zipEntry = new ZipEntry(item.getName());
                InputStream input = new FileInputStream(file);
                zip.putNextEntry(zipEntry);
                IOUtils.copy(input, zip);
            }
            zip.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取报告信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:list')")
    @GetMapping("/getReport")
    @ApiOperation("获取报告信息")
    public AjaxResult getReport(Report report){

        return AjaxResult.success(reportService.selectReportByProjectCodeAndDate(report));
    }

    /**
     * 获取报告信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:list')")
    @GetMapping("/getReportList")
    @ApiOperation("获取报告信息列表")
    public AjaxResult getReportList(Report report) throws ParseException {

        return AjaxResult.success(reportService.selectReportListByDate(report));
    }
    /**
     * 新增报告信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:list')")
    @Log(title = "报告信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增报告信息")
    public AjaxResult add(@RequestBody Report report)
    {
        return toAjax(reportService.insertReport(report));
    }


    /**
     * 查询报告信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(Report report)
    {
        startPage();
        List<Report> list = reportService.selectReportList(report);
        return getDataTable(list);
    }

    /**
     * 导出报告信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:export')")
    @Log(title = "报告信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Report report)
    {
        List<Report> list = reportService.selectReportList(report);
        ExcelUtil<Report> util = new ExcelUtil<Report>(Report.class);
        util.exportExcel(response, list, "工单信息数据");
    }

    /**
     * 获取报告信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(reportService.selectReportById(id));
    }


    /**
     * 修改报告信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:edit')")
    @Log(title = "报告信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Report report)
    {
        return toAjax(reportService.updateReport(report));
    }

    /**
     * 删除报告信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:report:list')")
    @Log(title = "报告信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {

        if(reportService.deleteReportByIds(ids) == 999){
            return AjaxResult.error("只能删除自己的报告哦!");
        }
        return  AjaxResult.success("报告删除成功!");
    }
}